package com.xindaoapp.happypet.fragments;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindao.photonics.fragments.manager.BaseFragment;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.activity.MainActivity;
import com.xindaoapp.happypet.bean.MyTasklist;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;

/**
 * 积分商城
 * 
 * @author Administrator
 * 
 */
public class IntegralFragment2 extends BaseFragment implements OnClickListener {

    private ListView vRenwu;
    private View mNoData;
    private PullToRefreshListView mPullToRefreshListView;

    // pic的数据
    public int[] pics = new int[] {R.drawable.jifen_qiandao, R.drawable.jifen_yaoqing,
            R.drawable.jifen_jinghua};
    private TextView vGoduihuan;
    private TextView vJinbi;

    // // 标题
    // public String[] titles = new String[] {"每日签到", "邀请好友", "话题加精华"};
    // // 金币数
    // public int[] jifens = new int[] {1, 20, 10};


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_integral, container, false);
    }

    @Override
    protected void initViews(View view) {
        super.initViews(view);
        vRenwu = (ListView) view.findViewById(R.id.lv_renwu);
        vGoduihuan = (TextView) view.findViewById(R.id.duihuan);
        vJinbi = (TextView) view.findViewById(R.id.jinbi);
        vGoduihuan.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.duihuan:
                Intent intent = new Intent(mActivity, MainActivity.class);
                intent.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                        ChangeCenterFragment.class.getCanonicalName());
                mActivity.startActivity(intent);
                break;

            default:
                break;
        }
    }

    @Override
    protected void initEvents(View view) {
        super.initEvents(view);
    }



    @Override
    protected void onLoadData() {
        super.onLoadData();
        getMoccaApi().getMyTasklist(new IRequest<List<MyTasklist>>() {
            //
            @Override
            public void request(List<MyTasklist> list) {
                // if (t != null && t.second != null) {
                // if (t.second.size() == 0) {
                // mNoData.setVisibility(View.VISIBLE);
                // } else {
                // mNoData.setVisibility(View.GONE);
                // mAdapter =
                // new MessageCenterAdapter(mActivity, t.second,
                // Constants.PAGE_SIZE, R.layout.item_message_center,
                // R.layout.item_loading);
                // if (t != null && t.containsKey(MoccaApi.RESPONSE_USERINFO)) {
                // String niuniu = (String) t.get(MoccaApi.RESPONSE_NIUNIU);
                // vJinbi.setText(niuniu);
                // List<MyTasklist> list = (List<MyTasklist>) t.get(MoccaApi.RESPONSE_ARRAY);
                vRenwu.setAdapter(new MyAdapter(list));
            }
            // }
            // onDataArrived(true);
            // } else {
            // onDataArrived(false);
            // }
            // mPullToRefreshListView.onRefreshComplete();
            // }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    protected String getTitleName() {
        return "积分商城";
    }



    @Override
    protected String getRightName() {
        return super.getRightName();
    }

    // 右边点击事件
    @Override
    protected void setRightViewListener(View view) {
        super.setRightViewListener(view);
        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // if (UserState.isLogged()) {
                // startActivity(new Intent(getActivity(), PushMessageActivity.class));
                Toast.makeText(mActivity, "右边点击跳转", 0).show();
                // }
            }
        });
    }


    @Override
    protected int getRightDrawable() {
        return R.drawable.jifen_center2;
    }



    @Override
    protected void setTopBarRightButtonDrawable(int resId) {
        super.setTopBarRightButtonDrawable(resId);
    }



    class MyAdapter extends BaseAdapter {

        List<MyTasklist> list = null;

        public MyAdapter(List<MyTasklist> list) {
            super();
            this.list = list;
        }


        public MyAdapter() {
            super();
        }


        @Override
        public int getCount() {

            return list.size();
        }

        @Override
        public Object getItem(int position) {

            return null;
        }

        @Override
        public long getItemId(int position) {

            return 0;
        }

        @Override
        public View getView(int position, View contentView, ViewGroup arg2) {
            contentView = View.inflate(mActivity, R.layout.item_integral_center, null);

            ImageView vPic = (ImageView) contentView.findViewById(R.id.iv_pic);
            TextView vTitle = (TextView) contentView.findViewById(R.id.title);
            TextView vJifen = (TextView) contentView.findViewById(R.id.tv_jifen);
            TextView vNum = (TextView) contentView.findViewById(R.id.tv_num);

            MyTasklist task = list.get(position);

            // vPic.setImageResource(pics[position]);
            ImageLoader.getInstance().displayImage(task.ico, vPic);
            vTitle.setText(task.title);
            vJifen.setText(task.reward + "");
            vNum.setText(task.cursetp + "/" + task.maxsetp);

            return contentView;
        }

    }

}
